<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Follow extends Eloquent {
	/*
	 * @author gaozongwei 关注表
	 */
	protected $table = 'follows';
	
	protected $fillable = array ('user_id', 'follow_id', 'follow_stat' );
	
	const FOLLOWED = 1;
	const FOLLOWCANLE = 0;
	
	/**
	 * 设置关注的状态
	 * 如果关注不存在，就先创建一个
	 * 
	 * @param integer $follow_id
	 * @param integer $follow_stat
	 * @return Follow
	 */
	public static function index($follow_id, $follow_stat) {
		$user = Auth::user();
		$result = Follow::where ( 'user_id', '=', $user->id )->where ( 'follow_id', $follow_id )->get ();
		if ( $result->count () ) {
			$result = $result->first ();
			if ($result->follow_stat != $follow_stat) {
				if ($follow_stat == self::FOLLOWED) {
					$user->increment('follows_count');
				} else {
					$user->decrement('follows_count');
				}
			}
			$result->follow_stat = $follow_stat;
			$result->touch();
			$result->save ();
		} else {
			$result = Follow::create ( array ('user_id' => $user->id, 'follow_id' => $follow_id, 'follow_stat' => $follow_stat ) );
			$result->save ();
			$user->increment('follows_count');
		}

		$user->touch();
		$user->save();
		return $result;
	}
	/*
	 * 关注作者
	 */
	public static function followed($follow_id) {
		return self::index ( $follow_id, self::FOLLOWED );
	}
	/*
	 * 取消关注
	 */
	public static function followCancle($follow_id) {
		return self::index ( $follow_id, self::FOLLOWCANLE );
	}
	/*
	 * 我关注人的列表
	 */
	public static function followerList($q, $rows_per_page, $offset, $exclude=array(), $cond = []){
		$query = User::join('follows', 'user.id', '=', 'follows.follow_id')
		->join('lawyer', 'lawyer.user_id', '=', 'user.id')
		->select(
				'follows.follow_id',
				'user.avatar',
				'user.realname'
		)
  		->whereNotIn('follows.follow_id', $exclude)
 		->where('follows.user_id', Auth::user()->id)
 		->where('follows.follow_stat', self::FOLLOWED)
 		->take($rows_per_page)
 		->skip($offset);
		
 		if ($q) {
			$query = $query->where('user.realname', 'like', "%$q%");
 		}

		if (isset($cond['city'])) {
			$query = $query->where('user.city', '=', $cond['city']);
		}
		if (isset($cond['cat_id'])) {
			$scope = $cond['cat_id'];
			$query = $query->where('lawyer.work_scope', 'like', "%$scope%");
		}
		if (isset($cond['experience'])) {
			$query = $query->where('lawyer.practice_period', '=', $cond['experience']);
		}
 		
		return $query->get();
	}
	/*
	 * 关注我的人列表
	 */
	public static function fansList($exclude=array()){
		return User::leftjoin('follows', 'user.id', '=', 'follows.user_id')
				->select(
						'follows.user_id',
						'user.avatar',
						'user.realname'
						)
				->whereNotIn('follows.user_id', $exclude)
				->where('follows.follow_id', Auth::user()->id)
				->where('follows.follow_stat', self::FOLLOWED)
				->get();
	}
 
	/*
	 * 测试看现登陆用户是否已经关注了follow的用户
	 */
	public static function isFollowing($follow_id) {
		if (!Auth::check()) {
			return null;
		}
		$user = Auth::user();
		$result = Follow::where ( 'user_id', '=', $user->id )->where ( 'follow_id','=', $follow_id )->where('follow_stat', self::FOLLOWED)->get ();
		return (bool)$result->count();
	}
	
	public function user() {
		return $this->belongTo ( 'User' );
	}

}
